package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.WasteTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.WasteTypeForm;
import com.youlai.boot.system.model.query.WasteTypeQuery;
import com.youlai.boot.system.model.vo.WasteTypeVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 垃圾类型管理前端控制层
 *
 * @author youlaitech
 * @since 2025-02-22 16:33
 */
@Tag(name = "垃圾类型管理接口")
@RestController
@RequestMapping("/api/v1/wasteTypes")
@RequiredArgsConstructor
public class WasteTypeController  {

    private final WasteTypeService wasteTypeService;

    @Operation(summary = "垃圾类型管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:wasteType:query')")
    public PageResult<WasteTypeVO> getWasteTypePage(WasteTypeQuery queryParams ) {
        IPage<WasteTypeVO> result = wasteTypeService.getWasteTypePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增垃圾类型管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:wasteType:add')")
    public Result<Void> saveWasteType(@RequestBody @Valid WasteTypeForm formData ) {
        boolean result = wasteTypeService.saveWasteType(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取垃圾类型管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('sys:wasteType:edit')")
    public Result<WasteTypeForm> getWasteTypeForm(
        @Parameter(description = "垃圾类型管理ID") @PathVariable Long id
    ) {
        WasteTypeForm formData = wasteTypeService.getWasteTypeFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改垃圾类型管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:wasteType:edit')")
    public Result<Void> updateWasteType(
            @Parameter(description = "垃圾类型管理ID") @PathVariable Long id,
            @RequestBody @Validated WasteTypeForm formData
    ) {
        boolean result = wasteTypeService.updateWasteType(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除垃圾类型管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:wasteType:delete')")
    public Result<Void> deleteWasteTypes(
        @Parameter(description = "垃圾类型管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = wasteTypeService.deleteWasteTypes(ids);
        return Result.judge(result);
    }
}
